<script type="text/ecmascript-6">
  import { mapGetters } from 'vuex'
  export default{
     data(){
         return{
             tabList:[],
//             dataList:{
//               0: [],
//               1: [],
//               2: [],
//               3: [],
//               4: [],
//               5: [],
//               6: []
//             },
             dataList:{},
             id: this.$route.params.id,
             tabIndex:0,
             flex:false,
             activity_type: '',
             types:[],
             heights:"100%",
             isDrag:false,
             title:""
         }
     },
    methods:{
      loadMore(){
          if(!this.tabList[this.tabIndex].hasMore){
              return this.$refs.pulldown[this.tabIndex].onLoadUp();
          }
          const page =this.tabList[this.tabIndex].page+1;
          this.$store.dispatch('queryChubList',{number:this.id,type:this.tabList[this.tabIndex].activity_type,index:this.tabIndex,method:'up',page:page});
      },
      refresh(){
          this.$store.dispatch('queryChubList',{number:this.id,type:this.tabList[this.tabIndex].activity_type,index:this.tabIndex,method:'down'});
      },
      go(id){
          window.location.href="http://m.dev.tnf.ibrand.cc/activity/#!/registration/activity_detail/"+id;
      },
      change(i){
          const type=this.tabList[i].activity_type;
          this.tabIndex=i;
          const url = '#!/chub/list/'+this.id+"/"+type;
          window.history.replaceState('chub_list','文章列表',url);
          if( !this.tabList[i].init ){
            this.$store.dispatch('queryChubList',{number:this.id,type:type,index:i});
          }
          else{
            this.tabList[i].noList = !!!this.dataList[i].length;
          }
      },
      jump_detail(id) {
        this.$router.push({ name: 'chub_detail', params: {id: id} });

      },
       setTitle(title){
          this.title=title;
       },
       CompleteChubList(res,index,method){
           let list,
               page = res.meta.pagination,
               current_page = page.current_page,
               total_pages =page.total_pages;
           if( current_page ==1){
               list =res.data;
           }else{
               list = this.dataList[index].concat(res.data);
           }
           console.log(index);
//           this.dataList[index] =list;
           this.$set(this.dataList,index,list);
           this.tabList[index].init=true;
           this.tabList[index].page=current_page;
           this.tabList[index].hasMore = current_page < total_pages;
           this.tabList[index].noList = !!!list.length;
           if(method && method=="down"){
               this.$refs.pulldown[this.tabIndex].onLoadDown();
//             this.$refs.pull.onLoadDown();
           }
           else{
              this.$refs.pulldown[this.tabIndex].onLoadUp();
           }
       },
       CompleteActivityType(res){
           this.activity_type = res;
           this.activityType();
       },
       activityType(){
           const that=this;
           this.tabList=[
             {
                 name:'全部',
                 page: 0,
                 hasMore: true,
                 init: false,
                 activity_type: 0,
                 noList: false
             }
           ];
           that.activity_type.forEach( (val,i)=>{
               let o={
                  page:0,
                  hasMore:true,
                  init:false,
                  noList:false
               };
               o.name=val.name;
               o.activity_type=val.id;
               that.tabList.push(o);
               that.types.push(val.id);
           });
           console.log(this.tabList);
         let type = Number(this.$route.params.type);
         if( type!=0 ){
             this.tabIndex = this.types.indexOf(type) +1;
         }
         let id =  Number(this.$route.params.id);
         this.$store.dispatch('queryChubList',{number:id,type:type,index:this.tabIndex});
       }
    },
    created(){
      EventBus.$on("complete-chubList",this.CompleteChubList);
      EventBus.$on("complete-activity-type",this.CompleteActivityType);
      EventBus.$on("set-title",this.setTitle);
      this.$store.dispatch('queryChutActivityType');
    },
    mounted(){
//      this.$store.dispatch('queryChubList',number,type);
    }
//    activated(){
//      EventBus.$on("complete-chubList",this.CompleteChubList);
//      EventBus.$on("complete-activity-type",this.CompleteActivityType);
//      EventBus.$on("set-title",this.setTitle);
//      this.$store.dispatch('queryChutActivityType');
//    }
  }
</script>
